<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/image/tcms_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_images_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');


class Image_Delete_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_image/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'delete'), 'delete_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'delete_confirmed'), 'delete_confirmed_event');
	}


	protected function delete_event($image_id) {
		$dbc = $this->get_dbc();

		$image = new TCMS_Image_DBC();
		if ($image->load_by_id($dbc, $image_id)) {
			$ci_ids = TCMS_CI_Image_DBC::select_ci_ids_and_type_for_image($dbc, $image_id);
			$this->export('cis', $this->prepare_cis($ci_ids));

			$this->export('image', $image);

			$view = $this->get_view('default');
		} else {
			$view = $this->create_redirect_view('admin/modules/tangra_cms/image/images.php');
		}

		return $view;
	}


	protected function delete_confirmed_event($image_id) {
		$dbc = $this->get_dbc();

		$this->remove_image_files($dbc, $image_id);

		try {
			$dbc->start_trans();
			TCMS_Image_DBC::delete_cascade($dbc, $image_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/image/images.php');

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Unexpected parameter.');
	}


	private function remove_image_files(DB_Connection $dbc, $image_id) {
		$image = new TCMS_Image_DBC();
		if ($image->load_by_id($dbc, $image_id)) {

			$settings_images = new TCMS_Settings_Images_DBC();
			if ($settings_images->load_by_id($dbc, 1)) {
				$file_path = $this->_config->get_uploads_dir().$image->get_path();

				$main_image = $file_path.$image->get_filename().'.jpg';
				if(file_exists($main_image)) {
					unlink($main_image);
				}

				$orginal_file = $file_path.$image->get_filename().TCMS_Image::ORIGINAL_IMAGE_SUFFIX . '_' . $image->get_orig_filename();
				if(file_exists($orginal_file)) {
					unlink($orginal_file);
				}

				$fixed_width_file = $file_path.$image->get_filename().TCMS_Image::FIXED_SIZE_IMAGE_SUFFIX .'.jpg';
				if (file_exists($fixed_width_file)) {
					unlink($fixed_width_file);
				}

				$t1_file = $file_path.$image->get_filename().TCMS_Image::THUMBNAIL1_IMAGE_SUFFIX.'.jpg';
				if (file_exists($t1_file)) {
					unlink($t1_file);
				}

				$t2_file = $file_path.$image->get_filename().TCMS_Image::THUMBNAIL2_IMAGE_SUFFIX.'.jpg';
				if (file_exists($t2_file)) {
					unlink($t2_file);
				}
			} else {
				throw new Tangra_Module_Exception('Cannot load settings images');
			}
		}
	}


	private function prepare_cis($cis) {
		$ret = array();
		$hidden_inc_dir = $this->get__config()->get_hidden_dir().'inc/';

		$dbc = $this->get_dbc();

		foreach($cis as $ci_id => $type_id) {
			$type = new TCMS_CI_Type_DBC();
			if ($type->load_by_id($dbc, $type_id)) {
				$class_name = $type->get_class_name();
				if (!class_exists($class_name)) {
					require_once($hidden_inc_dir.$type->get_class_path());
				}

				$ci = new $class_name;
				$ci->load_by_id($dbc, $ci_id);
				$ret[$ci_id]['text'] = $ci->get_as_short_plain_text();
				$ret[$ci_id]['manage_path'] = $type->get_page_manage_path();
			}
		}

		return $ret;
	}
}
